#!/usr/bin/python
#coding=gbk

'''
Created on Aug 2, 2010

@author: ting
'''

import unittest
from urllib2 import urlopen
from weibopy.qqAuth import OAuthHandler, BasicAuthHandler
from weibopy.qqApi import API

class Test(unittest.TestCase):
    
    consumer_key = "b6b3ee6703f1407b9615166757ac0a05"
    consumer_secret = "4b1e6f495caf9497ca978451a4734279"
    
    def __init__(self):
            """ constructor """
    
    def getAtt(self, key):
        try:
            return self.obj.__getattribute__(key)
        except Exception, e:
            print e
            return ''
        
    def getAttValue(self, obj, key):
        try:
            return obj.__getattribute__(key)
        except Exception, e:
            print e
            return ''
    def setToken(self, token, tokenSecret):
        self.auth = OAuthHandler(self.consumer_key, self.consumer_secret, secure=True)
        self.auth.setToken(token, tokenSecret)
        self.api = API(self.auth, api_root='/api')
        
    def basicAuth(self, source, username, password):
        self.auth = BasicAuthHandler(username, password)
        self.api = API(self.auth,source=source)
        
    def auth(self):
        self.auth = OAuthHandler(self.consumer_key, self.consumer_secret, callback='null', secure=True)
        auth_url = self.auth.get_authorization_url()
        print 'Please authorize: ' + auth_url
        verifier = raw_input('PIN: ').strip()
        self.auth.get_access_token(verifier)
        #self.api = API(self.auth)
        
    def home_timeline(self):
        timeline = self.api.home_timeline(reqnum=20)
        for line in timeline:
            self.obj = line
            mid = self.getAtt("id")
            text = self.getAtt("text")
            print "friends_timeline---"+ str(mid) +":"+ text

test = Test()
test.setToken('029a9a51354f4fee9816a91858dba40a', 'c6493429755188736c0630e9c3698024')
test.home_timeline()
